<?php

namespace app\common;

use app\admin\model\BusinessLog as BusinessLogModel;
use \think\facade\Request;
use think\Service;

/**
 * 业务日志
 */
class BusinessLog extends Service
{
    /**
     * 请求参数
     * @var mixed
     */
    protected $param;

    /**
     * 操作员id
     * @var
     */
    protected $operator_id;

    /**
     * 请求地址
     * @var
     */
    protected $url;

    /**
     * 请求IP
     * @var string
     */
    protected $ip;

    protected $module_name;

    /**
     * 初始化
     */
    public function __construct()
    {
        $this->param = Request::param();
        $this->operator_id = Request()->operator_id ?? Request()->user_id;
        $this->url = Request()->url();
        $this->ip = Request()->ip();
        $this->module_name = app()->http->getName();
    }

    /**
     * 记录操作日志(后面再迭代)
     * @param int|array|null $id
     * @param string $business_module 业务模块
     * @param string $operate 操作类型
     * @param string $content 备注
     * @param string $user_type 1 erp  2 官网
     * @param string $child_business_module 子业务模块
     * @param string $child_entity_id 子业务模块id
     * @param string $operate $approval_job  审批岗位
     * @return void
     * 2024-11-12 16 14 21
     * author: jiang
     */
    public function logOperation($id, string $business_module = null, string $operate = null, string $content = '',$user_type = 1,$child_business_module='',$child_entity_id=0,$approval_job='')
    {
        // 如果业务模块为null 则使用URL
        $business_module = $business_module ?? $this->url;
        // 如果operate为null 则使用URL
        $operate = $operate ?? $this->url;
        $child_entity_id = $child_entity_id ? $child_entity_id:$id;
        $child_business_module = $child_business_module ? $child_business_module:$business_module;
        $insert_data = [];
        if (is_int($id) || is_null($id) || is_string($id)) {
            $insert_data[] = [
                'user_id' => $this->operator_id,
                'operate' => $operate,
                'user_type' => $user_type,
                'business_module' => $business_module,
                'child_business_module' => $child_business_module,
                'child_entity_id' => $child_entity_id,
                'approval_job' => $approval_job,
                'entity_id' => $id,
                'ip_address' => $this->ip,
                'content' => $content,
                'create_time' => date('Y-m-d H:i:s'),
                'module_name' => $this->module_name
            ];
            $this->handle($insert_data);
        } elseif (is_array($id)) {
            foreach ($id as $item) {
                $insert_data[] = [
                    'user_id' => $this->operator_id,
                    'operate' => $operate,
                    'user_type' => $user_type,
                    'business_module' => $business_module,
                    'entity_id' => $item,
                    'child_business_module' => $child_business_module,
                    'child_entity_id' => $child_entity_id,
                    'approval_job' => $approval_job,
                    'ip_address' => $this->ip,
                    'content' => $content,
                    'create_time' => date('Y-m-d H:i:s'),
                    'module_name' => $this->module_name
                ];

                $this->handle($insert_data);
            }
        }
    }

    /**
     * 批量
     * @param $insert_data
     * @return void
     * 2024-11-12 16 24 46
     * author: jiang
     */
    protected function handle($insert_data)
    {
        $result = BusinessLogModel::insertALl($insert_data);
    }
    /**
     * 审核状态修改
     * @param $where
     * @param $id
     * @return void
     * 2024-11-12 16 24 46
     * author: jiang
     */
    public function  updateApprover($where,$id)
    {
        $update= ['approver'=>$id,'processed'=>1,'process_time'=>date('Y-m-d H:i:s')];
        BusinessLogModel::where($where)->order('id desc')->limit(1)->update($update);
    }
}
